#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
class Solution
{
public:
    // 预处理一个前缀和矩阵
    vector<vector<int>> matrixBlockSum(vector<vector<int>> &mat, int k)
    {
        int m = mat.size(), n = mat[0].size();
        int sum[110][110] = {0};
        for (int i = 1; i <= m; ++i)
            for (int j = 1; j <= n; ++j)
                sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + mat[i - 1][j - 1];
        vector<vector<int>> ans;
        ans.resize(m);
        for (int i = 0; i < m; ++i)
        {
            ans[i].resize(n);
            for (int j = 0; j < n; ++j)
            {
                int ti = i + 1, tj = j + 1;
                // 把左上点和右下点的坐标全部都求出来
                int x1 = ti - k >= 1 ? ti - k : 1;
                int y1 = tj - k >= 1 ? tj - k : 1;
                int x2 = ti + k > m ? m : ti + k;
                int y2 = tj + k > n ? n : tj + k;
                ans[i][j] = sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1];
            }
        }
        return ans;
    }
};